<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>LimitIterator 类</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="iteratoriterator.valid.html">« IteratorIterator::valid</a></li>
      <li style="float: right;"><a href="limititerator.construct.html">LimitIterator::__construct »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="spl.iterators.html">迭代器</a></li>
    <li>LimitIterator 类</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="class.limititerator" class="reference">
 <h1 class="title">LimitIterator 类</h1>
 
 
 <div class="partintro"><p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7, PHP 8)</p>
 

  <div class="section" id="limititerator.intro">
   <h2 class="title">简介</h2>
   <p class="para">
    <span class="classname"><strong class="classname">LimitIterator</strong></span> 类允许遍历一个
    <span class="classname"><a href="class.iterator.html" class="classname">Iterator</a></span> 的限定子集的元素。
   </p>
  </div>

 
  <div class="section" id="limititerator.synopsis">
   <h2 class="title">类摘要</h2>
 

   <div class="classsynopsis">
    <div class="ooclass">
     
    </div>

    <div class="classsynopsisinfo">
     <span class="ooclass">
      <span class="modifier">class</span> <strong class="classname">LimitIterator</strong>
     </span>

     <span class="ooclass">
      <span class="modifier">extends</span>
       <a href="class.iteratoriterator.html" class="classname">IteratorIterator</a>
     </span>
     {</div>

    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 方法 */</div>
    <div class="constructorsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><a href="limititerator.construct.html" class="methodname">__construct</a></span>(<span class="methodparam"><span class="type"><a href="class.iterator.html" class="type Iterator">Iterator</a></span> <code class="parameter">$iterator</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$offset</code><span class="initializer"> = 0</span></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$limit</code><span class="initializer"> = -1</span></span>)</div>

    <div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="limititerator.current.html" class="methodname">current</a></span>(): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="limititerator.getinneriterator.html" class="methodname">getInnerIterator</a></span>(): <span class="type"><a href="class.iterator.html" class="type Iterator">Iterator</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="limititerator.getposition.html" class="methodname">getPosition</a></span>(): <span class="type">int</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="limititerator.key.html" class="methodname">key</a></span>(): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="limititerator.next.html" class="methodname">next</a></span>(): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="limititerator.rewind.html" class="methodname">rewind</a></span>(): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="limititerator.seek.html" class="methodname">seek</a></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$offset</code></span>): <span class="type">int</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="limititerator.valid.html" class="methodname">valid</a></span>(): <span class="type">bool</span></div>


    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 继承的方法 */</div>
    <div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="iteratoriterator.current.html" class="methodname">IteratorIterator::current</a></span>(): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="iteratoriterator.getinneriterator.html" class="methodname">IteratorIterator::getInnerIterator</a></span>(): <span class="type"><span class="type">?</span><span class="type"><a href="class.iterator.html" class="type Iterator">Iterator</a></span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="iteratoriterator.key.html" class="methodname">IteratorIterator::key</a></span>(): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="iteratoriterator.next.html" class="methodname">IteratorIterator::next</a></span>(): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="iteratoriterator.rewind.html" class="methodname">IteratorIterator::rewind</a></span>(): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="iteratoriterator.valid.html" class="methodname">IteratorIterator::valid</a></span>(): <span class="type">bool</span></div>

   }</div>
 
  </div>
 

 
 
  <div class="section" id="limititerator.examples">
   <h2 class="title">范例</h2>
   <p class="para">
    <div class="example" id="example-3629">
     <p><strong>示例 #1 <span class="classname"><strong class="classname">LimitIterator</strong></span> usage example</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">//&nbsp;Create&nbsp;an&nbsp;iterator&nbsp;to&nbsp;be&nbsp;limited<br /></span><span style="color: #0000BB">$fruits&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">ArrayIterator</span><span style="color: #007700">(array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'apple'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'banana'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'cherry'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'damson'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'elderberry'<br /></span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">//&nbsp;Loop&nbsp;over&nbsp;first&nbsp;three&nbsp;fruits&nbsp;only<br /></span><span style="color: #007700">foreach&nbsp;(new&nbsp;</span><span style="color: #0000BB">LimitIterator</span><span style="color: #007700">(</span><span style="color: #0000BB">$fruits</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">)&nbsp;as&nbsp;</span><span style="color: #0000BB">$fruit</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$fruit</span><span style="color: #007700">);<br />}<br /><br />echo&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Loop&nbsp;from&nbsp;third&nbsp;fruit&nbsp;until&nbsp;the&nbsp;end<br />//&nbsp;Note:&nbsp;offset&nbsp;starts&nbsp;from&nbsp;zero&nbsp;for&nbsp;apple<br /></span><span style="color: #007700">foreach&nbsp;(new&nbsp;</span><span style="color: #0000BB">LimitIterator</span><span style="color: #007700">(</span><span style="color: #0000BB">$fruits</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">)&nbsp;as&nbsp;</span><span style="color: #0000BB">$fruit</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$fruit</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
string(5) &quot;apple&quot;
string(6) &quot;banana&quot;
string(6) &quot;cherry&quot;

string(6) &quot;cherry&quot;
string(6) &quot;damson&quot;
string(10) &quot;elderberry&quot;
</pre></div>
     </div>
    </div>
   </p>
  </div>
 
 
 </div>
 
 























































 
<h2>目录</h2><ul class="chunklist chunklist_reference"><li><a href="limititerator.construct.html">LimitIterator::__construct</a> — Construct a LimitIterator</li><li><a href="limititerator.current.html">LimitIterator::current</a> — Get current element</li><li><a href="limititerator.getinneriterator.html">LimitIterator::getInnerIterator</a> — Get inner iterator</li><li><a href="limititerator.getposition.html">LimitIterator::getPosition</a> — Return the current position</li><li><a href="limititerator.key.html">LimitIterator::key</a> — Get current key</li><li><a href="limititerator.next.html">LimitIterator::next</a> — Move the iterator forward</li><li><a href="limititerator.rewind.html">LimitIterator::rewind</a> — Rewind the iterator to the specified starting offset</li><li><a href="limititerator.seek.html">LimitIterator::seek</a> — Seek to the given position</li><li><a href="limititerator.valid.html">LimitIterator::valid</a> — Check whether the current element is valid</li></ul>
</div>
</div></div></body></html>